#!/usr/bin/env python
#-*- coding: utf-8 -*-
'''=========================
@Time : 2021/3/28 15:51
@Coder: xh
@IDE  : PyCharm
========================='''
import requests
import re
import json

'''
    Author: Face_to_sun
    modify: 2018-11-21
'''

def download_MP3(url):
    '''下载歌曲'''
    r = requests.get(url)
    with open('music/RedCape.mp3','wb') as f:
        f.write(r.content)

def getUrl():
    '''获得歌曲URL'''
    r = requests.post(url, headers=headers, data=data)
    print(r.content)
    response_data = json.loads(r.text)
    print(response_data)
    print(response_data['data'][0]['url'])
    download_url = response_data['data'][0]['url']
    return download_url

if __name__ == "__main__":
    url = 'https://music.163.com/weapi/privacy/info/get/v2?csrf_token='
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36',
        'Host': 'music.163.com',
        'referer': 'https://music.163.com/',
        'Origin': 'https://music.163.com'
    }

    data = {
        'params': 'dQ7QcRulV0Z7BEXXilRJFfXYqM7E5k3B30FiBjiqokvUD0VQIBEaph8vHkjHZVIh',
        'encSecKey': 'a505132a821a8335a443ac55e7541e2aab30e49f0beee3c6d9fe1565abf977cc0f6f99e1e602d123f1d6db4b762456bb8148f9dc95627ef3bcad96ad15cb3eb08c6f5342f09b470c4356b6ca81b8da7909e0663a363d7167e117fc887d9d2240a3db06df621fe91b11ff3ae2dcef5839979733fae87f7493841938be4a7507ed'
    }
    url = getUrl()
    download_MP3(url)

# params: dQ7QcRulV0Z7BEXXilRJFfXYqM7E5k3B30FiBjiqokvUD0VQIBEaph8vHkjHZVIh
# encSecKey: a505132a821a8335a443ac55e7541e2aab30e49f0beee3c6d9fe1565abf977cc0f6f99e1e602d123f1d6db4b762456bb8148f9dc95627ef3bcad96ad15cb3eb08c6f5342f09b470c4356b6ca81b8da7909e0663a363d7167e117fc887d9d2240a3db06df621fe91b11ff3ae2dcef5839979733fae87f7493841938be4a7507ed